package com.tian.service.impl;

import com.tian.mapper.ChargeUserCouponMapper;
import com.tian.message.UserCouponMessage;
import com.tian.service.UserUseCouponService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * {@code @description:} 使用优惠券
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-10-14 22:38
 * {@code @version:} 1.0
 */
@Service
public class UserUseCouponServiceImpl implements UserUseCouponService {
    @Resource
    private ChargeUserCouponMapper chargeUserCouponMapper;

    @Override
    public boolean usingCoupon(UserCouponMessage userCouponMessage) {
        if (CollectionUtils.isEmpty(userCouponMessage.getUserCouponIdList())) {
            return true;
        }
        chargeUserCouponMapper.using(userCouponMessage.getUserCouponIdList());
        return true;
    }

    @Override
    public boolean usedCoupon(UserCouponMessage userCouponMessage) {
        if (!CollectionUtils.isEmpty(userCouponMessage.getUserCouponIdList())) {
            chargeUserCouponMapper.used(userCouponMessage.getUserCouponIdList());
            return true;
        }
        if (userCouponMessage.getUserId() != null) {

        }
        return true;
    }

    @Override
    public boolean freeUsingCoupon(UserCouponMessage userCouponMessage) {
        if (userCouponMessage.getUserId() == null) {
            return true;
        }
        chargeUserCouponMapper.freeByUserId(userCouponMessage.getUserId());
        return true;
    }
}
